package cn.itcast.fore.web.action;

import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.Constants;
import cn.itcast.bos.utils.MailUtils;
import cn.itcast.crm.domain.Customer;
import cn.itcast.fore.web.action.common.BaseAction;

@Controller // bean组件
@Scope("prototype") // 多例
public class CustomerAction extends BaseAction<Customer> {
	// 注入jmsQueueTemplate Q的消息队列模版对象
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsQueueTemplate;

	// 发送验证码
	@Action("customer_sendCheckcode")
	public String sendCheckcode() {
		// 生成四位随机数字
		final String checkcode = RandomStringUtils.randomNumeric(4);
		// 将验证码放入session
		ServletActionContext.getRequest().getSession().setAttribute(model.getMobilePhone(), checkcode);
		// 调用MQ生产短消息
		jmsQueueTemplate.send("bos.sms.normal.checkcode", new MessageCreator() {

			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				// 因为我们要获取两个值 所以用MapMessage
				MapMessage mapMessage = session.createMapMessage();
				// 放入验证码
				mapMessage.setString("checkcode", checkcode);
				// 放入手机号
				mapMessage.setString("mobilePhone", model.getMobilePhone());
				return mapMessage;
			}
		});
		// 调用阿里大鱼发送验证码的短信
		System.out.println("手机号是：" + model.getMobilePhone() + "的验证码：" + checkcode);

		return NONE;

	}

	// 属性驱动获取验证码
	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	// 注入redis模版
	private RedisTemplate redisTemplate;

	public void setRedisTemplate(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	// 客户注册
	@Action(value = "customer_regist", results = { @Result(type = REDIRECT, location = "/signup-success.html"), @Result(name = INPUT, type = REDIRECT, location = "/signup.html") })
	public String regist() {
		// 第一步：验证码校验
		// 获取session中验证码
		String checkcodeSession = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getMobilePhone());
		// 清除session验证码
		ServletActionContext.getRequest().getSession().removeAttribute(model.getMobilePhone());
		// 获取表单的验证码,属性驱动
		// 对比验证码不为空并且从session缓存保存的验证码与从form表单获取的不一样
		if (StringUtils.isBlank(checkcode) || !checkcodeSession.equals(checkcode)) {
			// 验证码校验失败
			return INPUT;
		}

		// 第二步：用户数据保存（远程）
		// WebClient.create("http://localhost:9002/crm_management/services/customerservice/customers")
		WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerservice/customers").type(MediaType.APPLICATION_JSON).post(model);
		// 第三步：发送邮件并将邮件激活码存入redis
		String activeUrl = Constants.BOS_FORE_URL + "/customer_activeMail";// 激活地址
		// String activeUrl =
		// "http://localhost:9003/bos_fore/customer_activeMail";//激活地址(servlet)
		String activecode = RandomStringUtils.randomNumeric(32);
		String content = "<h3>请点击地址激活:<a href=" + activeUrl + "?activecode=" + activecode + "&mobilePhone=" + model.getMobilePhone() + ">" + activeUrl + "</a></h3>";
		String subject = "BOS测试邮件shhm010";// 主题
		String to = model.getEmail();// 邮件地址
		MailUtils.sendMail(subject, content, to);// 发邮件
		// 注入redis缓存和设定时间24小时
		redisTemplate.opsForValue().set(model.getMobilePhone(), activecode, 24, TimeUnit.HOURS);
		return SUCCESS;

	}

	// 属性驱动封装邮箱激活码
	private String activecode;

	public void setActivecode(String activecode) {
		this.activecode = activecode;
	}

	@Action("customer_activeMail")
	public String activeMail() throws Exception {
		// 判断邮箱激活码和手机号在session缓存中不为空
		if (StringUtils.isNotBlank(activecode) && StringUtils.isNoneBlank(model.getMobilePhone())) {
			// 可以激活
			// 在 redis中获取激活码
			String activecodeRedis = (String) redisTemplate.opsForValue().get(model.getMobilePhone());
			// redis中存放激活码和传过来的激活码一样，则允许激活
			if (StringUtils.isNotBlank(activecodeRedis) && activecode.equals(activecodeRedis)) {
				// 远程调用crm系统，更新type字段
				// 远程调用crm系统，更新type字段
				// WebClient.create("http://localhost:9002/crm_management/services/customerservice/customers")
				WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerservice/customers").path("/type").path("/" + model.getMobilePhone()).path("/" + 1).type(MediaType.APPLICATION_JSON).put(null);
				System.out.println("用户激活成功！" + model.getMobilePhone());
				// 清除redis激活码
				redisTemplate.delete(model.getMobilePhone());
				// 响应页面
				// 指定编码集
				ServletActionContext.getResponse().setContentType("text/html; charset=UTF-8");
				ServletActionContext.getResponse().getWriter().print("恭喜激活成功！请从主页登录");
			} else {
				// 激活失败
				System.out.println("用户激活失败！1" + model.getMobilePhone());
				// 响应页面
				ServletActionContext.getResponse().setContentType("text/html; charset=UTF-8");
				ServletActionContext.getResponse().getWriter().print("激活失败！请重新发送激活邮件");

			}
		} else {
			// 激活失败
			System.out.println("用户激活失败！2" + model.getMobilePhone());
			// 响应页面
			ServletActionContext.getResponse().setContentType("text/html; charset=UTF-8");
			ServletActionContext.getResponse().getWriter().print("激活失败！请重新注册");

		}
		return NONE;

	}
}
